<template>
  <el-dialog :title="info.title" :visible.sync="dialogVisible" width="20%" :show-close="false">
    <strong>请输入类目名称：</strong>
    <el-input v-model="input" placeholder="请输入内容"></el-input>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitHandle">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: ['info', 'type'],
  data() {
    return {
      dialogVisible: false,
      input: '',
    }
  },
  methods: {
    submitHandle() {
      //新增分类 -- 1点击获取输入框值 2 隐藏弹框 3跟新视图
      if (this.type == 1) {
        console.log('新增', this.input);
        this.insertItemCategory({ cid: this.info.cid, name: this.input })
      } else if(this.type==2) {
        console.log('修改', this.input);
        this.updateCategory({ id: this.info.id, name: this.input })
      }else{
        console.log('新增一级分类', this.input);
        this.insertCategor({ name: this.input })
      }
    },
    //修改类目名称 id name
    async updateCategory(params) {
      let res = await this.$api.updateCategory(params)
      console.log('修改类目名称', res.data);
      if (res.data.status == 200) {
        this.$message({
          message: '恭喜你，修改成功',
          type: 'success'
        });
        this.dialogVisible = false;//隐藏弹框
        this.$emit('upDataView');//跟新父亲视图
        // this.$parent.itemCategory()//跟新父亲视图方法2
      }
    },
    //新增子级类目
    async insertItemCategory(params) {
      let res = await this.$api.insertItemCategory(params)
      console.log('新增子级类目', res.data);
      if (res.data.status == 200) {
        this.$message({
          message: '恭喜你,新增成功',
          type: 'success'
        });
        this.dialogVisible = false;//隐藏弹框
        this.$emit('upDataView');//跟新父亲视图
        // this.$parent.itemCategory()//跟新父亲视图方法2
      }
    },
    //新增一级类目
    async insertCategor(params) {
      let res = await this.$api.insertCategory(params)
      console.log('新增一级类目', res);
      if (res.data.status == 200) {
        this.$message({
          message: '恭喜你，新增成功',
          type: 'success'
        });
        this.dialogVisible = false;//隐藏弹框
        this.$emit('upDataView');//跟新父亲视图
        // this.$parent.itemCategory()//跟新父亲视图方法2
      }
    },


  }
}
</script>

<style lang="less" scoped>
</style>